home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / strtok.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.1 KB  |  68 lines

  1. #include "lib.h"
  2.  
  3. /*
  4.  * Get next token from string s (NULL on 2nd, 3rd, etc. calls),
  5.  * where tokens are nonempty strings separated by runs of
  6.  * chars from delim.  Writes NULs into s to end tokens.  delim need not
  7.  * remain constant from call to call.
  8.  */
  9.  
  10. #ifdef NULL
  11. #undef NULL
  12. #endif
  13. #define    NULL    0
  14.  
  15. static char *scanpoint = NULL;
  16.  
  17. char *                /* NULL if no token left */
  18. strtok(s, delim)
  19. char *s;
  20. register _CONST char *delim;
  21. {
  22.     register char *scan;
  23.     char *tok;
  24.     register _CONST char *dscan;
  25.  
  26.     if (s == NULL && scanpoint == NULL)
  27.         return(NULL);
  28.     if (s != NULL)
  29.         scan = s;
  30.     else
  31.         scan = scanpoint;
  32.  
  33.     /*
  34.      * Scan leading delimiters.
  35.      */
  36.     for (; *scan != '\0'; scan++) {
  37.         for (dscan = delim; *dscan != '\0'; dscan++)
  38.             if (*scan == *dscan)
  39.                 break;
  40.         if (*dscan == '\0')
  41.             break;
  42.     }
  43.     if (*scan == '\0') {
  44.         scanpoint = NULL;
  45.         return(NULL);
  46.     }
  47.  
  48.     tok = scan;
  49.  
  50.     /*
  51.      * Scan token.
  52.      */
  53.     for (; *scan != '\0'; scan++) {
  54.         for (dscan = delim; *dscan != '\0';)    /* ++ moved down. */
  55.             if (*scan == *dscan++) {
  56.                 scanpoint = scan+1;
  57.                 *scan = '\0';
  58.                 return(tok);
  59.             }
  60.     }
  61.  
  62.     /*
  63.      * Reached end of string.
  64.      */
  65.     scanpoint = NULL;
  66.     return(tok);
  67. }
  68.